package cn.weakup.designpattern.d_behavior.strategy.usepattern;

import java.math.BigDecimal;

/**
 * @author lixj
 * @version 1.0.0
 * @date 2022/6/2
 * @desc 策略控制类
 */
public class Context<T> {
    private ICouponDiscount<T> couponDiscount;
    private T couponInfo;

    public Context(CouponEnum couponType, T couponInfo){
        this.couponInfo = couponInfo;
        ICouponDiscount couponDiscount = null;
        switch (couponType.getType()){
            case 1:
                couponDiscount = new EnoughCouponDiscount();
                break;
            case 2:
                couponDiscount = new JustDiscountCouponDiscount();
                break;
            case 3:
                couponDiscount = new PercentCouponDiscount();
                break;
            case 4:
                couponDiscount = new JustAnyCouponDiscount();
                break;
            default:
                throw new UnsupportedOperationException("未知优惠类型");
        }
        this.couponDiscount = couponDiscount;
    }

    public BigDecimal discountAmount(BigDecimal totalPrice) {
        return couponDiscount.discountAmount(this.couponInfo, totalPrice);
    }
}
